package com.springboot.cloud.demos.feign.rest;

import com.alibaba.fastjson.JSONObject;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.sensitive.SensitiveFilter;
import com.springboot.cloud.demos.entity.form.MomentCommentForm;
import com.springboot.cloud.demos.entity.po.User;
import com.springboot.cloud.demos.feign.config.BusConfig;
import com.springboot.cloud.demos.feign.events.BusSender;
import com.springboot.cloud.demos.feign.service.MomentCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class MomentCommentController extends BaseController {

    @Autowired
    private MomentCommentService momentCommentService;
    @Autowired
    private BusSender busSender;
    @Autowired
    private SensitiveFilter sensitiveFilter;

    @RequestMapping(value = "/momentComment", method = RequestMethod.POST)
    public Result save(HttpServletRequest request, @RequestBody MomentCommentForm momentCommentForm) {
        User user = getUser(request);
        momentCommentForm.setUserId(user.getId());

// 待过滤的句子
        String sentence = momentCommentForm.getContent();
// 进行过滤
        String filted = sensitiveFilter.filter(sentence, '*');
        momentCommentForm.setContent(filted);
        Result result =momentCommentService.save(momentCommentForm);

        if (result.isSuccess()) {
            //新建推送和消息
            String message= JSONObject.toJSONString(momentCommentForm);
            busSender.send(BusConfig.EXCHANGE_NAME, "comment.message",message);
        }
        return result;
    }

    @RequestMapping(value = "open/momentComment/query", method = RequestMethod.GET)
    public Result query(@RequestParam(value = "momentId")String momentId, @RequestParam(value = "pageNo",defaultValue = "1" ) Integer pageNo, @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        return momentCommentService.query(momentId,pageNo,pageSize);
    }
    @RequestMapping(value = "open/momentComment/queryCount", method = RequestMethod.GET)
    public Result queryCount(@RequestParam(value = "momentId")String momentId) {
        return momentCommentService.queryCount(momentId);
    }

}
